<?php

class User_Model_User extends Database_Table_Row
{
	
	/**
	 * get current user id
	 */
	public function getId()
	{
		return $this -> user_id;
	}

	function getTitle()
	{
		return $this -> name;
	}


	

	/**
	 * set user password
	 * @param string $string
	 * @return User_Model_User
	 */
	public function setPassword($string)
	{
		$this -> password_salt = Text::random(8);
		$this -> password_hash = $this -> _createPassword($string, $this -> password_salt);
		return $this;
	}

	/**
	 * check if string is password
	 * @param string $string
	 * @return bool
	 */
	public function isPassword($string)
	{
		return (bool)($this -> password_hash === $this -> _createPassword($string, $this -> password_salt));
	}

	/**
	 * generate password from 2 string
	 * @param string $password_string
	 * @param string $hash_string
	 * @return string
	 */

	protected function _createPassword($password_string, $hash_string)
	{
		return sha1($password_string . $hash_string);
	}

	/**
	 * @return bool
	 */
	public function isSuper()
	{
		return $this -> role_id && $this -> role_id < 2;
	}

	/**
	 * @return bool
	 */
	public function isAdmin()
	{
		return $this -> role_id && $this -> role_id < 3;
	}

	/**
	 * @return bool
	 */
	public function isModerator()
	{
		return $this -> role_id && $this -> role_id < 4;
	}

	/**
	 * @return bool
	 */
	public function isEditor()
	{
		return $this -> role_id && $this -> role_id < 5;
	}

	/**
	 * @return bool
	 */
	public function isStaff()
	{
		return $this -> role_id && $this -> role_id < 6;
	}

	/**
	 * @return bool
	 */
	public function isMember()
	{
		return $this -> role_id && $this -> role_id < 7;
	}

	public function getEmail()
	{
		return $this -> email;
	}

	public function isApproved()
	{
		return $this -> is_approved;
	}

	public function isVerified()
	{
		return $this -> is_verified;
	}

	public function isActive()
	{
		return $this -> is_active;
	}

	function login()
	{
		$request = Request::initiator();

		$this -> lastlogin_ip = $request -> getClientIp();
		$this -> lastlogin_at = time();
		$this -> save();
	}

	function getHref()
	{
		return Agilet::getUrl('user/profile/me/', $this -> getId());
	}

}
